﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartMap.Domain;
using SmartMap.Domain.Controllers;
using SmartMap.Domain.Common;

namespace SmartMap.admin
{
	public partial class AdminMaster : System.Web.UI.MasterPage
	{
		protected void Page_PreRender(object sender, EventArgs e)
		{
            if (AdminController.IsAuthenticated)
			{
				LoginSuccess();
			}
			else
			{
				ShowLoggedOut();
			}
		}

		protected void LoginButton_Click(object sender, EventArgs e)
		{
            AdminController.Login(UserName.Text, Password.Text);
            if (AdminController.IsAuthenticated)
			{
				LoginSuccess();
			}
			else
			{
				ShowLoggedOut();
				LoginErrorContainer.Visible = true;
			}
		}

		protected void LogoutButton_Click(object sender, EventArgs e)
		{
			AdminController.LogOut();
		}

		private void LoginSuccess()
		{
			LoggedContainer.Visible = true;
			LoggedOutContainer.Visible = false;
			LoginErrorContainer.Visible = false;
			UserNameLabel.Text = Admin.Current.AdminUserName;
            this.RegisterJavaScriptLink("~/admin/scripts/ComponentLoader.js");
		}

		private void ShowLoggedOut()
		{
			LoggedContainer.Visible = false;
			LoggedOutContainer.Visible = true;
		}
	}
}